<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    slash<phpunion@gmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: info_supplier.class.php 2012-04-24 19:21:33 $
 * @Show Note: 供应商管理
 */
Class info_supplier extends TheAdmin
{
		/* DataBase object */
		public $db;
	
		/* View the data page limit items */
		public $pageSize = 10;

		public $user_info = array();
		
		/**
		 *	Construct this module class.
		*/
		public function info_supplier()
		{
		}
		public function set()
		{
			$this->TheAdmin();
			//定义模版
			$this->tplFile ="info_supplier.tpl";
			$this->admin_user_id = $this->userRequest->get('admin_user_id');
			//取得数据库连接
			$this->db = Factory::getDB("info_supplier");
			//取得用户对于该类的权限
			$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
			$class_p_arr = $this->show_p();
			//取得该类的权值数组
			$class_method = $class_p_arr['class_method'];
			//取得用户操作菜单
			$menu = array();
			foreach ($class_method as $k=>$v)
			{
				if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
				{
					array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
				}
			}
			
			/** 取得用户信息 **/
			$this->user_info = $this->get_user_info($this->admin_user_id);

			//用户有权菜单生成 可以添加其他操作
			$this->smarty->assign('menus',$menu);
			$this->smarty->assign("main_menus",$this->get_user_menus());
		}
	
		/**
		 *	Call class construct method.
		*/
		public function __construct()
		{
				$this->info_supplier();
		}
		public function _display()
		{
			$this->smarty->display($this->tplFile);
		}
		/**
		 *	default method.
		*/
		public function show()
		{
			$ord = isset($_GET['ord']) ? trim($_GET['ord']) : '';
			if('' == $ord) {
				$col = "supplier_id";
			} else {
			    $col =  $ord;

				$co = isset($_GET['co']) ? intval($_GET['co']) : 0;
				if($co == 0) {
					$this->db->setOrderBy();
					$co = 1;
				} else {
				    $co = 0;
				}
				$this->smarty->assign('co',$co);
			}

			$supplier_name = isset($_POST['supplier_name']) ? trim($_POST['supplier_name']) : '';

			/* 所属部门 */
			$depert = $this->user_info['user_department_tree'];
				
				//$where = 1;
				$where = "supplier_department_tree = '{$depert}'";
				if('' != $supplier_name) {
					$where .=" and supplier_name like '%$supplier_name%'";
					$this->smarty->assign('supplier_name',$supplier_name);
				}

				$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
				//数据库执行sql语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if( 0 > $rowNo) goToIndex($rowNo);
				//分页
				$page = Factory::getBaseClass('pagination');
				$page->set($rowNo,$this->pageSize);
				$numrows = $this->pageSize;
				$offset  = $page->getSqlMove();
				$arr = $this->db->getAll($where,$col,$numrows,$offset);
				admin_log($this->admin_user_id,__METHOD__,'查看',1);
				//数据库执行select语句出错
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				if(0 > $arr ) goToIndex($arr);
		
				$this->smarty->assign('arr',$arr);
				//默认方法
				$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
				$this->smarty->assign('curPage',$page->result['curPage']);
				$this->smarty->assign('act','list');
				$this->smarty->display($this->tplFile);
		}
		
		/**
		 *	add method.
		*/
		public function add()
		{
				//添加数据
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$this->smarty->assign('act_title','添加');
						$this->smarty->assign('act','add');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						$temp = $this->getValue();
						$insrt = $this->db->insert_info_supplier($temp);
						$error_no = 0 > $insrt ?  $insrt :'-1501';
						admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						alertMsg($error_no, 'index.php?c=info_supplier');
				}
		}
		
		/**
		 *	edit method.
		*/
		public function edit()
		{
				//取得id，也就是唯一索引
				$supplier_id = isset($_REQUEST['supplier_id']) ? intval(trim($_REQUEST['supplier_id'])) : 0 ;
				//取得是否查看详情
				$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
				if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
				{
						//使用模版，展示提交界面
						$result = $this->db->getOne($supplier_id);
						if(empty($result))
						{
							alertMsg('-2001', 'index.php?c=info_supplier');
						}
						foreach ($result AS $k=>$v)
						{
							$this->smarty->assign("{$k}",$v);
						}
						$this->smarty->assign('is_show',$is_show);
						$this->smarty->assign('act_title','修改');
						$this->smarty->assign('act','edit');
						$this->smarty->display($this->tplFile);
				} else {
						//用户提交，把数据插入数据库
						//设置where条件
						$where = "supplier_id=$supplier_id";
						$temp = $this->getValue(1);
						$update = $this->db->update_info_supplier($temp,$where);
						admin_log($this->admin_user_id,__METHOD__,$where,2);
						$error_no = 0 > $update ?  $update :'-1502';
						//取得错误的sql语句
						//$error_sql = end($this->db->getDebugMsg());
						//echo $error_sql;
						alertMsg($error_no, 'index.php?c=info_supplier');
				}
		}
		
		/**
		 *	del method.
		*/
		public function del()
		{
				//取得id，也就是唯一索引
				$supplier_id = isset($_REQUEST['supplier_id']) ? intval(trim($_REQUEST['supplier_id'])) : 0 ;
				$where = "supplier_id=$supplier_id";
				$del=$this->db->delete_info_supplier($where);
				admin_log($this->admin_user_id,__METHOD__,$where,3);
				$error_no = 0 > $del ?  $del :'-1503';
				//取得错误的sql语句
				//$error_sql = end($this->db->getDebugMsg());
				//echo $error_sql;
				alertMsg($error_no, 'index.php?c=info_supplier');
		}
		
		/**
		 *	get request method.
		*/
		public function &getValue($isEdit=0)
		{
			$temp = array();
			$check = Factory::getBaseClass("checkBase");
			if($isEdit){
				$temp['supplier_id'] = $_POST['supplier_id'];
			} else {
				$temp['supplier_id'] = "";
				$temp['supplier_department_tree'] = trim($this->user_info['user_department_tree']);				
				$temp['admin_user_id'] = $this->admin_user_id;
			}
			//补充其他的字段
			$temp['supplier_name'] = $_POST['supplier_name'];
			$temp['supplier_address'] = $_POST['supplier_address'];
			$temp['supplier_tel'] = $_POST['supplier_tel'];
			$temp['supplier_fox'] = $_POST['supplier_fox'];
			$temp['supplier_zip'] = $_POST['supplier_zip'];
			$temp['supplier_url'] = $_POST['supplier_url'];
			$temp['supplier_weibo'] = $_POST['supplier_weibo'];
			$temp['supplier_info'] = $_POST['supplier_info'];
			
			$temp['supplier_im'] = $_POST['supplier_im']; //即时通讯
			$temp['supplier_cooperation'] = $_POST['supplier_cooperation'];  //合作情况
			/*
			$temp['supplier_department_tree'] = $_POST['supplier_department_tree'];
			$temp['laster'] = $_POST['laster'];			
			$temp['admin_user_id'] = $_POST['user_id'];
			*/
			$temp['laster'] = trim($this->user_info['admin_user_id']);
			$temp['update_time'] = time();

			admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
			return $temp;
		}
		/**
		 *	get class method and popedom.
		*/
		public function show_p()
		{
			//该方法为，提供用户使用方法定义，非web用户权限相关不用写
			return array(
				/* 定义该类中文名称*/
				'class_name'=>array(__CLASS__,'供应商管理'),
				'class_method' =>array(
					//array（方法名，中文解释，权值,是否为菜单）
					'show'  => array('show','列表',1,true),
					'add'  => array('add','添加',2,true),
					'edit'  => array('edit','编辑',4,false),
					'del'  => array('del','删除',8,false),
					'show_p'=> array('show_p','',0,false)
				)
			);
		}
}

?>